package com.example.word.adapter

import android.content.Intent
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Switch
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.word.R
import com.example.word.data.room.Word
import com.example.word.viewModels.WordViewModel
import kotlinx.android.synthetic.main.cell_normal.view.*

// 比较数据内容
val diffCallback = object : DiffUtil.ItemCallback<Word>() {
    override fun areItemsTheSame(oldItem: Word, newItem: Word) = oldItem == newItem

    override fun areContentsTheSame(oldItem: Word, newItem: Word): Boolean =
        oldItem.id == oldItem.id
}

class WordAdapter(
    private val useCardView: Boolean,
    private val wordViewModel: WordViewModel
) : ListAdapter<Word, WordAdapter.WordViewHolder>(diffCallback) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WordViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val itemView =
            if (useCardView)
                layoutInflater.inflate(R.layout.cell_card, parent, false)
            else
                layoutInflater.inflate(R.layout.cell_normal, parent, false)
        val holder = WordViewHolder(itemView)
        holder.apply {
            itemView.setOnClickListener {
                val url = Uri.parse("https://m.youdao.com/dict?le=eng&q=${textViewEnglish.text}")
                val intent = Intent(Intent.ACTION_VIEW)
                intent.data = url
                it.context.startActivity(intent)
            }
            switchChineseInvisible.setOnCheckedChangeListener { _, b ->
                val word: Word = itemView.getTag(R.id.word_for_view_holder) as Word
                if (b) {
                    word.chineseInvisible = b
                    textViewChineseMeaning.visibility = View.GONE
                } else {
                    word.chineseInvisible = b
                    textViewChineseMeaning.visibility = View.VISIBLE
                }
                wordViewModel.updateWords(word)
            }
        }
        return holder
    }


    override fun onBindViewHolder(viewHolder: WordViewHolder, position: Int) {
        val word = getItem(position)
        viewHolder.apply {
            itemView.setTag(R.id.word_for_view_holder, word)
            textViewId.text = (position + 1).toString()
            textViewEnglish.text = word.word
            textViewChineseMeaning.text = word.chineseMeaning
            if (word.chineseInvisible) {
                textViewChineseMeaning.visibility = View.GONE
                switchChineseInvisible.isChecked = true
            } else {
                textViewChineseMeaning.visibility = View.VISIBLE
                switchChineseInvisible.isChecked = false
            }
        }
    }

    inner class WordViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textViewId: TextView = itemView.textViewNumber
        val textViewEnglish: TextView = itemView.textViewEnglish
        val textViewChineseMeaning: TextView = itemView.textViewChineseMeaning
        val switchChineseInvisible: Switch = itemView.switchChineseInvisible
    }

}